<?php

namespace App\Http\Controllers\v1;


use App\Libs\Sms;
use App\Models\Casebook;
use App\Models\MiniProgram;
use App\Models\Org;
use App\Models\Project;
use App\Models\Training;
use App\Models\User;
use App\Models\UserMessage;
use App\Models\UserProject;
use App\Models\UserProjectOrder;
use App\Models\UserTrans;
use App\Models\WechatTempMsg;
use EasyWeChat\Core\Exception;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use EasyWeChat\Foundation\Application;


class WeixinController extends Controller
{


    //wechat
    public function payCallback(Request $request)
    {
        file_put_contents('/tmp/1.txt', file_get_contents('PHP://input'));

        $xml = simplexml_load_string($request->getContent(),'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
        $sn = $xml->out_trade_no;

        $trans = UserTrans::where('sn', $sn)->first();
        $orgInfo = Org::find($trans->org_id);

        $options = config('wechat');
        $options['app_id'] = $orgInfo->wechat_app_id;
        $options['secret'] = $orgInfo->wechat_app_secret;
        $options['payment']['merchant_id'] = $orgInfo->wechat_payment_merchant_id;
        $options['payment']['key'] = $orgInfo->wechat_payment_key;

        $app = new Application($options);

        $response = $app->payment->handleNotify(function($notify, $successful){

            if($successful)
            {
                $trans = UserTrans::where('sn', $notify->out_trade_no)->first();

                DB::beginTransaction();
                try{
                    DB::table('user_trans')->where('sn', $notify->out_trade_no)->update(['status'=>1, 'pay_time'=>date('Y-m-d H:i:s')]);

                    $pInfo = $trans->desc;

                    $userProject = new UserProject();
                    $data['user_id'] = $trans->user_id;
                    $data['project_id'] = $pInfo['project_id'];
                    $data['verify_status'] = 0;
                    $data['extends'] = $pInfo['extends'];
                    $data['payment'] = $pInfo['payment'];
                    $userProject->fill($data)->save();

                    //微信模版消息，团队管理员收到用户申请加入活动
                    WechatTempMsg::add(50, ['project_id'=>$pInfo['project_id'], 'user_project_id'=>$userProject->id]);



                    $data = [];
                    $userProOrd = new UserProjectOrder();
                    $data['org_id'] = $trans->org_id;
                    $data['user_id'] = $trans->user_id;
                    $data['project_id'] = $userProject->project_id;
                    $data['user_project_id'] = $userProject->id;
                    $data['amount'] = $trans->amount;
                    $data['goods_info'] = $pInfo['payment'];
                    $data['pay_time'] = date('Y-m-d H:i:s');
                    $data['status'] = 0;
                    $data['user_trans_id'] = $trans->id;

                    $userProOrd->fill($data)->save();


                    DB::commit();

                } catch(\Illuminate\Database\QueryException $ex) {
                    DB::rollback();
                    return false;
                }
            }
            return true;
        });
        return $response;
    }
}
